package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Competencia;
import com.lostreseditores.sabersai.model.dto.CompetenciaDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class CompetenciaView {
    private InputText txtComciadescripv;
    private InputText txtComciasiglav;
    private InputText txtComciadn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<CompetenciaDTO> data;
    private CompetenciaDTO selectedCompetencia;

    public CompetenciaView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            CompetenciaDTO competenciaDTO = (CompetenciaDTO) e.getObject();

            if (txtComciadescripv == null) {
                txtComciadescripv = new InputText();
            }

            txtComciadescripv.setValue(competenciaDTO.getComciadescripv());

            if (txtComciasiglav == null) {
                txtComciasiglav = new InputText();
            }

            txtComciasiglav.setValue(competenciaDTO.getComciasiglav());

            if (txtComciadn == null) {
                txtComciadn = new InputText();
            }

            txtComciadn.setValue(competenciaDTO.getComciadn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtComciadescripv != null) {
            txtComciadescripv.setValue(null);
            txtComciadescripv.setDisabled(true);
        }

        if (txtComciasiglav != null) {
            txtComciasiglav.setValue(null);
            txtComciasiglav.setDisabled(true);
        }

        if (txtComciadn != null) {
            txtComciadn.setValue(null);
            txtComciadn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Competencia entity = null;

        try {
            Integer comciadn = new Integer(txtComciadn.getValue().toString());
            entity = BusinessDelegatorView.getCompetencia(comciadn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtComciadescripv.setDisabled(false);
            txtComciasiglav.setDisabled(false);
            txtComciadn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtComciadescripv.setValue(entity.getComciadescripv());
            txtComciadescripv.setDisabled(false);
            txtComciasiglav.setValue(entity.getComciasiglav());
            txtComciasiglav.setDisabled(false);
            txtComciadn.setValue(entity.getComciadn());
            txtComciadn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveCompetencia(FacesUtils.checkString(
                    txtComciadescripv), FacesUtils.checkInteger(txtComciadn),
                FacesUtils.checkString(txtComciasiglav));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteCompetencia(FacesUtils.checkInteger(
                    txtComciadn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateCompetencia(FacesUtils.checkString(
                    txtComciadescripv), FacesUtils.checkInteger(txtComciadn),
                FacesUtils.checkString(txtComciasiglav));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtComciadn == null) {
                txtComciadn = new InputText();
            }

            txtComciadn.setValue(selectedCompetencia.getComciadn());

            BusinessDelegatorView.deleteCompetencia(FacesUtils.checkInteger(
                    txtComciadn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedCompetencia);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String comciadescripv, Integer comciadn,
        String comciasiglav) throws Exception {
        try {
            BusinessDelegatorView.updateCompetencia(comciadescripv, comciadn,
                comciasiglav);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("CompetenciaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtComciadescripv() {
        return txtComciadescripv;
    }

    public void setTxtComciadescripv(InputText txtComciadescripv) {
        this.txtComciadescripv = txtComciadescripv;
    }

    public InputText getTxtComciasiglav() {
        return txtComciasiglav;
    }

    public void setTxtComciasiglav(InputText txtComciasiglav) {
        this.txtComciasiglav = txtComciasiglav;
    }

    public InputText getTxtComciadn() {
        return txtComciadn;
    }

    public void setTxtComciadn(InputText txtComciadn) {
        this.txtComciadn = txtComciadn;
    }

    public List<CompetenciaDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataCompetencia();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<CompetenciaDTO> competenciaDTO) {
        this.data = competenciaDTO;
    }

    public CompetenciaDTO getSelectedCompetencia() {
        return selectedCompetencia;
    }

    public void setSelectedCompetencia(CompetenciaDTO competencia) {
        this.selectedCompetencia = competencia;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
